<?php

namespace backend\modules\business\models;

use common\models\business\BusinessContractPurchase;
use common\models\sys\SysSetting;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\business\BusinessContract;
use yii\db\Query;

/**
 * BusinessContractSearch represents the model behind the search form about `common\models\business\BusinessContract`.
 */
class BusinessContractSearch extends BusinessContract
{

    //状态
    public $status = null;

    //是否要在往来汇总中合计，有的合同没有购进单结算单，不显示去掉干扰
    public $htwlshow = 0;


    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'cateid', 'start_time', 'end_time', 'final_num','htwlshow'], 'integer'],
            [['name', 'no', 'partyA', 'partyB','status'], 'safe'],
            [['amount', 'advance_ratio', 'final_amount'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = BusinessContract::find()->alias('c');

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }


        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'cateid' => $this->cateid,
            'advance_ratio' => $this->advance_ratio,
            'start_time' => $this->start_time,
            'end_time' => $this->end_time,
            'final_amount' => $this->final_amount,
            'final_num' => $this->final_num,
        ]);

        if($this->htwlshow == 1){
            $subquery = (new Query())->select('id')->from(['cp'=>BusinessContractPurchase::tableName()])->where('cp.contract_id=c.id');
            $query->andFilterWhere(['exists',$subquery]);
        }

        //按照合同的过期状态搜索
        $setoutdata = SysSetting::getValue('contract_out_time');
        $outdate = $setoutdata*3600*24 + time();
        if($this->status == BusinessContract::STATUS_NOMAL && $this->status != null){
            $query->andOnCondition('end_time>:outdate',[':outdate'=>$outdate]);
        }else if($this->status == BusinessContract::STATUS_OUT){
            $query->andOnCondition('end_time<:now',[':now'=>time()]);
        }else if($this->status == BusinessContract::STATUS_WILLOUT){
            $query->andOnCondition('end_time>:now and end_time<:outdate',[':now'=>time(),':outdate'=>$outdate]);
        }

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'no', $this->no])
            ->andFilterWhere(['like', 'partyA', $this->partyA])
            ->andFilterWhere(['like', 'partyB', $this->partyB]);

        return $dataProvider;
    }
}
